package com.pdg.islandinterloper.database;

import java.util.ArrayList;
import java.util.List;

import com.pdg.islandinterloper.MainActivity;
import com.pdg.islandinterloper.database.interfaces.TableProvider;
import com.pdg.islandinterloper.database.tableproviders.IslandTableProvider;
import com.pdg.islandinterloper.database.tableproviders.ProfileTableProvider;
import com.pdg.islandinterloper.interfaces.ActivityClient;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class IslandInterloperDatabase extends SQLiteOpenHelper implements ActivityClient<MainActivity>{

	private static final String DATABASE_NAME = "IslandInterloper.db";
	private static final int DATABASE_VERSION = 1;
	private MainActivity activity;
	private List<TableProvider> tableProviders = new ArrayList<TableProvider>();
	private ProfileTableProvider profileTableProvider;
	private IslandTableProvider islandTableProvider;

	public IslandInterloperDatabase(MainActivity theActivity) {
		super(theActivity, DATABASE_NAME, null, DATABASE_VERSION);
		setActivity(theActivity);
		setProfileTableProvider(new ProfileTableProvider(this));
		setIslandTableProvider(new IslandTableProvider(this));
	}
	
	private void setIslandTableProvider(IslandTableProvider theIslandTableProvider) {
		islandTableProvider = theIslandTableProvider;
		addTableProvider(islandTableProvider);
	}

	private void setProfileTableProvider(
			ProfileTableProvider theProfileTableProvider) {
		profileTableProvider = theProfileTableProvider;
		addTableProvider(profileTableProvider);
	}
	
	public IslandTableProvider getIslandTableProvider(){
		return islandTableProvider;
	}
	
	public ProfileTableProvider getProfileTableProvider(){
		return profileTableProvider;
	}

	protected void addTableProvider(TableProvider theTableProvider){
		tableProviders.add(theTableProvider);
	}

	private void setActivity(MainActivity theActivity) {
		activity = theActivity;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		for(TableProvider tableProvider: tableProviders){
			tableProvider.onCreate(db);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		for(TableProvider tableProvider: tableProviders){
			tableProvider.onUpgrade(db, oldVersion, newVersion);
		}
	}

	@Override
	public MainActivity getActivity() {
		return activity;
	}

}
